/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.model.command;

import static com.cea.papyrus.diagram.sequence.sedi.model.util.ModelUtil.getPackagedElements;

import org.eclipse.uml2.uml.Event;

/**
 * Structure that indicates the position of an Event.
 *
 * @author frankd@ifi.uio.no
 */
public class EventPosition extends PackagedElementPosition {
	public Event event;
	
	public EventPosition() {
	}
	public EventPosition(final Event event) {
		if (event != null 
			&& event.getNearestPackage() != null 
			&& event.getNearestPackage().getPackagedElements().indexOf(event) >= 0) {
			setEvent(event);
			setPackage(event.getNearestPackage());		
			setPosition(getPackage().getPackagedElements().indexOf(event));
		}
	}
	
	public Event getEvent() {
		return event;
	}
	public void setEvent(final Event event) {
		this.event = event;
	}

	/**
	 * Adds the event to the model at the specified position.
	 */
	public void addEvent() {
		getPackagedElements(getPackage()).add(getPosition(), getEvent());
	}
	
	/**
	 * Removes the event from the model.
	 */
	public void removeEvent() {
		assert getPackagedElements(getPackage()).indexOf(getEvent()) == getPosition();
		getPackagedElements(getPackage()).remove(getPosition());
	}
}
